package zozopo.manual.dialog;

import java.io.IOException;
import java.sql.Connection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import zozopo.database.BusiDatabaseManager;
import zozopo.database.DatabaseManager;

import zozopo.manual.operations.ContentOS;
import zozopo.user.operations.UserOS;
import zozopo.user.operations.UserTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class DeleteContentAction extends ActionSupport implements Action,SessionAware,ServletRequestAware,ServletResponseAware{
	
	//传入参数
	private int contentId;  //账号,可以用email 或用户
	private String password;
	
	//传出参数
	private String responseText;
	private Boolean isSuccess=true;
	
	
	
	
	public String execute(){
		DatabaseManager databaseManager = new BusiDatabaseManager();
		Connection conn = null;
			
		responseText = getValidateInfo();
		
		if(responseText==null){
			try{
				conn = databaseManager.getConnection();
				ContentOS contentOS = new ContentOS(conn);
				
				isSuccess = contentOS.drop(contentId);
				
				
			}finally{
				if(conn != null){
					databaseManager.releaseConnection(conn);
				}
			}
			responseText = "删除成功.";
		}
			
		
		
		
		try {
			
			response.getWriter().append("{\"responseText\":\"");
			response.getWriter().append(responseText);
			response.getWriter().append("\",\"isSuccess\":\"");
			response.getWriter().append(isSuccess.toString());
			response.getWriter().append("\"}");
			response.getWriter().flush();
			response.getWriter().close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return NONE;
	}
	
	
	public String getValidateInfo() {
		
		
		if(password == null){
			isSuccess = false;
			responseText = "密码不能为空.";
			return responseText;
		}
		
		
		UserTB userInfo = (UserTB)request.getSession().getAttribute("userInfo");
		
		if(userInfo==null){
			isSuccess = false;
			responseText = "请登陆后再做操作.";
			return responseText;
		}
		
		
		DatabaseManager databaseManager = new BusiDatabaseManager();
		Connection conn = null;
		
		try{
			conn = databaseManager.getConnection();
			
			UserOS userOS = new UserOS(conn);
			ContentOS contentOS = new ContentOS(conn);
			
			//当前用户验证
			if(!contentOS.isCurrentUser(contentId,userInfo.getName_user())){
				isSuccess = false;
				responseText = "非发布用户,无法删除文章.";
				return responseText;
			}
			
			
			if(!userOS.isRealUser(userInfo.getName_user(), password)){
				isSuccess = false;
				responseText = "输入确认密码错误,请重新输入.";
				return responseText;
			}
			
		}finally{
			databaseManager.releaseConnection(conn);
		}
			
			return responseText;
			
	}
	

	
	private HttpServletRequest request;
	private HttpServletResponse response;
	private Map<Object, Object> session;

	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		request = arg0;
	}

	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}

	

	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		response = arg0;
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=UTF-8");
	}


	

	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}


	public int getContentId() {
		return contentId;
	}


	public void setContentId(int contentId) {
		this.contentId = contentId;
	}


	public String getResponseText() {
		return responseText;
	}


	public void setResponseText(String responseText) {
		this.responseText = responseText;
	}


	public Boolean getIsSuccess() {
		return isSuccess;
	}


	public void setIsSuccess(Boolean isSuccess) {
		this.isSuccess = isSuccess;
	}

}
